/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2009-2020 Rock Mao
* creator : Rock Mao
* created : 2009-10-17   10:39
* filename: LaborD3D10Shader.h
-----------------------------------------------------------------------------
*/

#ifndef __LABORD3D10SHADER_H__
#define __LABORD3D10SHADER_H__
#include "LaborCoreDef.h"
#include "LaborD3D10Headers.h"
#include "Renderer/LaborShader.h"
#include "Renderer/LaborHardwareBuffer.h"
NAMESPACE_LABOR3D_BEGIN

class ILShaderParameters;
class CLD3D10ShaderParameters;
class CLD3D10Shader : public ILShader
{
protected:
	ID3D10ShaderReflection*          m_pShaderReflection;
	ID3D10Blob*                      m_pShaderCode;
	CLD3D10ShaderParameters*         m_pConstTable;
	std::wstring                     m_Name;
private:
	bool                             m_bIsLoad;
	eLShaderType                     m_eType;
public:
	CLD3D10Shader();
	virtual ~CLD3D10Shader();

	virtual bool         load( const wchar_t* fileName , unsigned long  arg );
	virtual bool         _load(const wchar_t* fileName , const unsigned char* buf , size_t bufLen, unsigned long arg)=0;

	virtual bool         unload();
	virtual bool         isLoaded() { return m_bIsLoad; }
	virtual eLShaderType type()   { return m_eType; }
	virtual bool         afterLoad(ID3D10Blob* pSrcCode);

	ILShaderParameters*     createShaderParamTable();
	virtual bool           installShaderConstTable(ILShaderParameters* pShaderParamBuffer) = 0;
	ILShaderParameters*     getShaderParamTable();
	void                   commitParamTable();
	unsigned long          memUsage();
};

enum   eLD3D10ResourceBindType;
class  CLD3D10HDBufReflection;
struct CLD3D10ShaderConstReflection : public ILShaderConstantReflection
{
	eLD3D10ResourceBindType    m_bindType;
	CLD3D10HDBufReflection*    m_pBufferReflection;
public:
	CLD3D10ShaderConstReflection();
	~CLD3D10ShaderConstReflection();
	bool setData(int _offset , void* _data , int dataLen);
	bool setData(void* _data , int dataLen);
	virtual  bool updateData() { return true; }
};

typedef std::vector<CLD3D10ShaderConstReflection*>     vecShaderConstantDescs;
typedef std::map<int , CLD3D10ShaderConstReflection*>  mapShaderConstantDescs;


class CLD3D10HDBufReflection : public ILHDBufferReflection
{
	vecShaderConstantDescs m_vConstant;
	char*                  m_pSysMem;
	int                    m_bindPoint;
	bool                   m_bDirty;
	size_t                 m_BufLen;
	CLHardwareBuffer*      m_pHDBuffer;
public:
	CLD3D10HDBufReflection(CLHardwareBuffer* pBuf , bool bSysBuffer , int bufLen);
	~CLD3D10HDBufReflection();
	size_t                       lenght();
	size_t                       nConstant();
	ILShaderConstantReflection*  constantDesc(size_t idx);
	//void                         addConstant(CLD3D10ShaderConstReflection* cd);
	CLD3D10ShaderConstReflection*   addConstant();
	void*                        systemBuffer();
	void                         setDirty(bool bDirty = true) {m_bDirty = bDirty; }
public:
	bool                         setData(int _off , void* _data , int dataLen);
	bool                         flushToBuffer();
};


NAMESPACE_LABOR3D_END
#endif // __LABORD3D10SHADER_H__